package net.wsfcp.android.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.bean.resultbean.UserBean;
import net.wsfcp.android.bean.resultbean.TabBean;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Map;

public class SpUtils {

    /*
     * 保存手机里面的名字
     */

    private static String FILE_NAME = "wsfcp_sp";


    //公钥
    final public static String PUBLIC_KEY = "public_key";
    //用户token
    final public static String TOKEN = "token";
    //用户Id
    final public static String USER_ID = "user_id";
    //七牛云图片token
    final public static String IMG_TOKEN = "img_token";
    //用户账号
    final public static String USERNAME = "username";
    //用户密码
    final public static String PASSWORD = "password";
    //是否登陆 1已登录0未登录
    final public static String LOGIN_STATE = "login_state";
    //如果已登录,改为用加密串登录
    final public static String ENCRYPT = "encrypt";
    //邮箱
    final public static String EMAIL = "email";
    //底部tab
    final public static String TAB = "tab";


    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor editor;
    private static SpUtils instance;

    public static SpUtils getInstance() {
        if (instance == null) {
            instance = new SpUtils();
        }
        return instance;
    }


    public SpUtils() {
        sharedPreferences = BaseApplication.getContext().getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    /**
     * 存储
     */
    public void put(String key, Object object) {
        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else {
            editor.putString(key, object == null ? "" : object.toString());
        }
        editor.commit();
    }

    /**
     * 获取保存的数据
     */
    public Object getSharedPreference(String key, Object defaultObject) {
        if (defaultObject instanceof String) {
            return sharedPreferences.getString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return sharedPreferences.getInt(key, (Integer) defaultObject);
        } else if (defaultObject instanceof Boolean) {
            return sharedPreferences.getBoolean(key, (Boolean) defaultObject);
        } else if (defaultObject instanceof Float) {
            return sharedPreferences.getFloat(key, (Float) defaultObject);
        } else if (defaultObject instanceof Long) {
            return sharedPreferences.getLong(key, (Long) defaultObject);
        } else {
            return sharedPreferences.getString(key, null);
        }
    }

    /**
     * 移除某个key值已经对应的值
     */
    public void remove(String key) {
        editor.remove(key);
        editor.commit();
    }

    /**
     * 清除所有数据
     */
    public void clear() {
        editor.clear();
        editor.commit();
    }

    /**
     * 查询某个key是否存在
     */
    public Boolean contain(String key) {
        return sharedPreferences.contains(key);
    }

    /**
     * 返回所有的键值对
     */
    public Map<String, ?> getAll() {
        return sharedPreferences.getAll();
    }


    //储存tab信息
    public void saveTab(ArrayList<TabBean> tabBeans) {
        Gson gson = new Gson();
        String obj = gson.toJson(tabBeans);
        put(SpUtils.TAB, obj);

    }

    //获取tab信息
    public ArrayList<TabBean> getTab() {
        String jsonTab = sharedPreferences.getString(TAB, "");
        if (jsonTab.length() == 0) {
            return null;

        } else {
            Type type = new TypeToken<ArrayList<TabBean>>() {
            }.getType();
            Gson gson = new Gson();
            ArrayList<TabBean> arrayList = gson.fromJson(jsonTab, type);

            return arrayList;

        }


    }


    //储存用户信息
    public void saveUserInfo(UserBean loginBean) {
        put(SpUtils.TOKEN, loginBean.getToken());
        put(SpUtils.USERNAME, loginBean.getUser_name());
        put(SpUtils.ENCRYPT, loginBean.getEncrypt());
        put(SpUtils.EMAIL, loginBean.getEmail());
        put(SpUtils.IMG_TOKEN, loginBean.getImages_token());
        put(SpUtils.USER_ID, loginBean.getId());
    }


    //获取公钥
    public String getPublicKey() {

        return sharedPreferences.getString(PUBLIC_KEY, "");

    }

    //获取用户token
    public String getToken() {

        return sharedPreferences.getString(TOKEN, "");

    }

    //获取七牛云token
    public String getImgToken() {

        return sharedPreferences.getString(IMG_TOKEN, "");

    }

    //获取用户账号
    public String getUserName() {

        return sharedPreferences.getString(USERNAME, "");

    }

    //获取用户Id
    public String getUserId() {

        return sharedPreferences.getString(USER_ID, "");

    }

    //获取用户密码
    public String getPassword() {

        return sharedPreferences.getString(PASSWORD, "");

    }

    //获取登录状态
    public int getLoginState() {

        return sharedPreferences.getInt(LOGIN_STATE, 0);

    }

    //获取加密串(用来自动登陆)
    public String getEncrypt() {

        return sharedPreferences.getString(ENCRYPT, "");

    }
}
